﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommandLine;
using System.IO;

namespace SPProfileData
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var options = new Options();
            if (!options.Parse(args))
            {
                Console.Out.Write(options.GetUsage());
                Environment.Exit(1);
            }

            CheckSanity(options);

            Console.Out.WriteLine(options.GetHeader().ToString());

            if (options.Import)
                Importer.Import(options.Url, options.FileName);
            else
                Exporter.Export(options.Url, options.FileName);

        }

        private static void CheckSanity(Options options)
        {
            var message = "";
            if (options.Import && options.Export)
            {
                message += "Cannot perform both import and export. Please only specify one option." + System.Environment.NewLine;
            }

            if (!options.Import && !options.Export)
            {
                message += "Please only specify either export (-e) or import (-i)." + System.Environment.NewLine;
            }

            if (options.Import && !File.Exists(options.FileName))
            {
                message += "The file to import does not exist." + System.Environment.NewLine;
            }

            if (!message.Equals(String.Empty))
            {
                Console.Out.Write(options.GetUsage());
                Console.Out.WriteLine(message);
                Environment.Exit(2);
            }
        }
    }
}
